﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.Models;
using Project_KhuyenMaiVang.DataProvider;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace Project_KhuyenMaiVang.DAO
{
    public class LienHeDAO : MyDataProvider
    {
        public LienHeDTO getLienHe(int idLienHe)
        {
            LienHeDTO result = new LienHeDTO();
            try
            {
                string strCommand = "SELECT* FROM LienHe WHERE MaLienHe = " + idLienHe.ToString();
                _Adapter = new SqlDataAdapter(strCommand, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                DataTable dt = dataset.Tables[0];
                object ob = GetDataFromDataRow(dt, 0);
                result = (LienHeDTO)ob;
            }
            catch (Exception ex)
            {
                ex.GetBaseException();
                result = null;
            }
            return result;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            LienHeDTO accountDTO = new LienHeDTO();
            accountDTO.MaLienHe = Int32.Parse(dt.Rows[i]["MaLienHe"].ToString());
            accountDTO.SoDienThoai = dt.Rows[i]["SoDienThoai"].ToString();
            accountDTO.Email = dt.Rows[i]["Email"].ToString();
            accountDTO.Facebook = (dt.Rows[i]["Facebook"].ToString());
            accountDTO.DiaChi = (dt.Rows[i]["DiaChi"].ToString());

            return (object)accountDTO;
        }

        public bool updateInfo(int idLienHe, string soDienThoai, string email, string facebook, string diaChi)
        {
            bool isOK = true;
            try
            {
                string updateCommand = "UPDATE LienHe " +
                                        " SET SoDienThoai = '" + soDienThoai + "', " +
                                        " Email = '" + email + "', " +
                                        " Facebook = '" + facebook + "'," +
                                        " DiaChi = N'" + diaChi
                                        + "' where MaLienHe = " + idLienHe.ToString();

                executeNonQuery(updateCommand);
            }
            catch
            {
                isOK = false;
            }

            return isOK;
        }
    }
}